package com.example.mysdktest.activities;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.example.mysdktest.R;
import com.example.mysdktest.base.BaseActivity;
import com.google.gson.Gson;
import com.xm9m.ibczy.dsp.beans.RTBResponseBean;
import com.xm9m.ibczy.dsp.utils.AntLog;
import com.xm9m.ibczy.dsp.views.BannerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by baichuan on 2016/11/18.
 */

public class Test02 extends BaseActivity implements View.OnClickListener {
    private static Gson gson = new Gson();

    private LinearLayout linar;
    private Button obt, obt2;
    private ListView lv;
    private List<View> vList = new ArrayList<>();
    private MyAdapter adapter;

    private Handler hand = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (123456 == msg.what) {
                addView((String) msg.obj);
            }
        }
    };

    @Override
    public int getLaout() {
        return R.layout.ac_test02;
    }

    @Override
    public void initView() {
        linar = (LinearLayout) findViewById(R.id.ac_test02_layout02_root);
        obt = (Button) findViewById(R.id.ac_test02_obt_01);
        obt2 = (Button) findViewById(R.id.ac_test02_obt_02);
        lv = (ListView) findViewById(R.id.ac_test02_lv_01);

    }

    public void addView(String value) {
        BannerView view = new BannerView(this);
        view.message.setText(value);
        vList.add(view);
        adapter.updata();
    }

    @Override
    public void initData() {
        adapter = new MyAdapter();
        lv.setAdapter(adapter);
    }

    @Override
    public void initListener() {
        obt.setOnClickListener(this);
        obt2.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ac_test02_obt_01:
                vList.add(new BannerView(this));
                adapter.updata();
                break;
            case R.id.ac_test02_obt_02:
               // MyTest.antTest03(this, this);
                break;
        }
    }

    public void getResponseData(RTBResponseBean.RTBResponse response) {
        AntLog.i("response=" + (null == response));
        if (null == response)
            return;
        AntLog.i("sid=" + response.getSid());
        for (RTBResponseBean.RTBResponse.AdInfo adInfo : response.getAdInfosList()) {
            AntLog.i("id" + adInfo.getId());
            AntLog.i("adid" + adInfo.getAdId());
            AntLog.i("createId" + adInfo.getCreativeId());
            AntLog.i("json=" + adInfo.getJson());
            Message message = new Message();
            message.obj = adInfo.getJson();
            message.what=123456;
            hand.sendMessage(message);
        }
        AntLog.i("debug=" + response.getDebugInfo());
        AntLog.i("processs=" + response.getProcessTimeMs());


    }


    class MyAdapter extends BaseAdapter {

        public void updata() {
            this.notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return vList.size();
        }

        @Override
        public Object getItem(int i) {
            return vList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return vList.size();
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            return vList.get(i);
        }
    }
}
